import tkinter as tk
import tkinter.messagebox
import pickle
import requests
# from tkinter import *
from bs4 import BeautifulSoup
from PIL import Image,ImageTk
import tkinter as tk
import io
import pickle
import tkinter.messagebox
import webbrowser

window = tk.Tk()
window.title('登录窗口')
window.geometry('450x300')

canvass = tk.Canvas(window, width=400, height=300)
imgs = Image.open('aa.jpg')
photos = ImageTk.PhotoImage(image=imgs)
canvass.create_image(40, 40, anchor='nw', image=photos)
canvass.pack()

tk.Label(window,text='用户名:').place(x=100,y=150)
tk.Label(window,text='密码:').place(x=100,y=190)

var_usr_name=tk.StringVar()
entry_usr_name=tk.Entry(window,textvariable=var_usr_name)
entry_usr_name.place(x=160,y=150)

var_usr_pwd=tk.StringVar()
entry_usr_pwd=tk.Entry(window,textvariable=var_usr_pwd,show='*')
entry_usr_pwd.place(x=160,y=190)

def usr_log_in():

    usr_name=var_usr_name.get()
    usr_pwd=var_usr_pwd.get()

    try:
        with open('usr_info.pickle','rb') as usr_file:
            usrs_info=pickle.load(usr_file)
    except FileNotFoundError:
        with open('usr_info.pickle','wb') as usr_file:
            usrs_info={'admin':'admin'}
            pickle.dump(usrs_info,usr_file)

    if usr_name in usrs_info:
        if usr_pwd == usrs_info[usr_name]:
            tk.messagebox.showinfo(title='welcome',
                                   message='欢迎您：'+usr_name)
            window.destroy()
            r = requests.get('https://ac.qq.com/Comic/all/theme/5/search/time/page/1')
            c = r.content


            result_list =[]

            soup = BeautifulSoup(c,'html.parser')
            all_img_list = soup.find('div',{'class':'ret-search-result'}).find_all('li')

            for item in all_img_list:
                d={}
                d['title']=item.find('div',{'class':'ret-works-cover'}).find('a')['title']
                d['img']=item.find('div',{'class':'ret-works-cover'}).find('img',{'class':'lazy'})['data-original']
                result_list.append(d)



            def showInfo(i):
                item = result_list[i]
                url =  item['img']
                img_r = requests.get(url)
                img_c = img_r.content
                data_stream = io.BytesIO(img_c)
                img = Image.open(data_stream)
                photo = ImageTk.PhotoImage(img)
                imgLabel.config( image=photo )
                imgLabel.image = photo
                titleLable.config(text= item['title'])

            global index
            index=0
            def nextPic():
                global index
                index +=1
                showInfo(index)

            def frPic():
                global index
                index -= 1
                showInfo(index)

            windows = tk.Tk()
            windows.title('LOL英雄')
            windows.geometry('1000x1000')

       

            imgLabel = tk.Label(windows)
            imgLabel.pack()
            titleLable = tk.Label(windows)
            titleLable.pack()

            nextBtn = tk.Button(windows,text='下一个',command=nextPic).place(x=530,y=650)
            frBtn = tk.Button(windows,text='上一个',command=frPic).place(x=430,y=650)


            showInfo(index)
            windows.mainloop()
        else:
            tk.messagebox.showerror(message='密码错误')

    elif usr_name=='' or usr_pwd=='' :
        tk.messagebox.showerror(message='用户名或密码为空')

    else:
        is_signup=tk.messagebox.askyesno('欢迎','您还没有注册，是否现在注册')
        if is_signup:
            usr_sign_up()

def usr_sign_up():
    
    def signtowcg():
        
        nn=new_name.get()
        np=new_pwd.get()
        npf=new_pwd_confirm.get()


        try:
            with open('usr_info.pickle','rb') as usr_file:
                exist_usr_info=pickle.load(usr_file)
        except FileNotFoundError:
            exist_usr_info={}           

        
        if nn in exist_usr_info:
            tk.messagebox.showerror('错误','用户名已存在')
        elif np =='' or nn=='':
            tk.messagebox.showerror('错误','用户名或密码为空')
        elif np !=npf:
            tk.messagebox.showerror('错误','密码前后不一致')

        else:
            exist_usr_info[nn]=np
            with open('usr_info.pickle','wb') as usr_file:
                pickle.dump(exist_usr_info,usr_file)
            tk.messagebox.showinfo('欢迎','注册成功')

            window_sign_up.destroy()

    window_sign_up=tk.Toplevel(window)
    window_sign_up.geometry('350x200')
    window_sign_up.title('注册')

    new_name=tk.StringVar()
    tk.Label(window_sign_up,text='用户名：').place(x=10,y=10)
    tk.Entry(window_sign_up,textvariable=new_name).place(x=150,y=10)

    new_pwd=tk.StringVar()
    tk.Label(window_sign_up,text='请输入密码：').place(x=10,y=50)
    tk.Entry(window_sign_up,textvariable=new_pwd,show='*').place(x=150,y=50)    

    new_pwd_confirm=tk.StringVar()
    tk.Label(window_sign_up,text='请再次输入密码：').place(x=10,y=90)
    tk.Entry(window_sign_up,textvariable=new_pwd_confirm,show='*').place(x=150,y=90)    

    bt_confirm_sign_up=tk.Button(window_sign_up,text='确认注册',
                                 command=signtowcg)
    bt_confirm_sign_up.place(x=150,y=130)

def usr_sign_quit():
    window.destroy()
# canvass = tk.Canvas(window, width=400, height=300)
# imgs = Image.open('aa.jpg')
# photos = ImageTk.PhotoImage(image=imgs)
# canvass.create_image(0, 0, anchor='nw', image=photos)
# canvass.pack()

bt_login=tk.Button(window,text='登录',command=usr_log_in)
bt_login.place(x=140,y=230)
bt_logup=tk.Button(window,text='注册',command=usr_sign_up)
bt_logup.place(x=210,y=230)
bt_logquit=tk.Button(window,text='退出',command=usr_sign_quit)
bt_logquit.place(x=280,y=230)

window.mainloop()